
import { Controller, Get, Post, Body, Patch, Param, Delete, Query, ParseIntPipe } from '@nestjs/common';
import { PaginationResult } from '@/common/interface/pagination.interface';
import { ApiTags } from '@nestjs/swagger';

import { MenuService } from './menu.service';
import { CreateMenuDto } from './dto/create-menu.dto';
import { UpdateMenuDto } from './dto/update-menu.dto';
import { QueryMenuDto } from './dto/query-menu.dto';
import { MenuEntity } from './entities/menu.entity';
import { ApiOperation } from '@nestjs/swagger'
import { UseGuards } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { Public } from '../../auth/auth.decorator';

// @Public() //JWT校验白名单装饰器
@ApiTags('menu')
@Controller('menu')
export class MenuController {
  constructor(private readonly service: MenuService) { }

  //获取菜单列表
  @Get()
  @ApiOperation({ summary: '获取menu列表' })
  async findAll(@Body() createDto) {
    return await this.service.findAllTree('menu');
  }

  //获取路由表
  @Get('route')
  @ApiOperation({ summary: '获取路由表(树结构)' })
  async getMenuTree() {
    const result = await this.service.findAllTree('route');
    // 可选：格式化返回结构
    return this.formatMenu(result);
  }

  private formatMenu(menus: any[]): any[] {
    return menus.map(menu => {
      return {
        path: menu.routePath,
        name: menu.routeName,
        component: menu.component,
        redirect: menu.redirect || undefined,
        meta: {
          title: menu.name,
          icon: menu.icon,
          hidden: !menu.visible ? true : false,
          keepAlive: menu.keepAlive ? true : false,
          alwaysShow: menu.alwaysShow ? true : false,//始终显示？
        },
        children: menu.children?.length ? this.formatMenu(menu.children) : [],
      };
    });
  }

  //创建菜单
  @Post()
  @ApiOperation({ summary: '创建menu' })
  create(@Body() createDto: CreateMenuDto) {
    console.log('6666666',createDto)
    return this.service.create(createDto);
  }

  //修改菜单
  @Patch(':id')
  @ApiOperation({ summary: '修改menu' })
  update(@Param('id', ParseIntPipe) id: number, @Body() updateMenuDto: UpdateMenuDto) {
    return this.service.update(id, updateMenuDto);
  }

  @Delete(':id')
  @ApiOperation({ summary: '删除menu' })
  remove(@Param('id', ParseIntPipe) id: number) {
    return this.service.remove(id);
  }
}
